package com.xinsoft.controller.mes;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sun.star.awt.DeviceInfo;
import com.xinsoft.aop.ano.SysLog;
import com.xinsoft.common.BusinessType;
import com.xinsoft.common.Response;
import com.xinsoft.constant.Const;
import com.xinsoft.entity.dto.MesWorkDayDTO;
import com.xinsoft.entity.dto.MesWorkDayParam;
import com.xinsoft.entity.po.CusDevice;
import com.xinsoft.entity.po.MesDateDevice;
import com.xinsoft.entity.po.MesWorkDate;
import com.xinsoft.service.CusDeviceService;
import com.xinsoft.service.MesDateDeviceService;
import com.xinsoft.service.MesWorkDateService;
import com.xinsoft.validator.ValidatorUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName MesWorkDateController
 * @Description 工作日历控制层
 * @Author Li9527
 * @Date 2023-8-28 10:53:27
 * @Version 1.0
 */
@RestController
@RequestMapping("/mes/workDate")
@Api(value = "MesWorkDateController", tags = {"工作日历控制层"})
public class MesWorkDateController {

	@Autowired
	private MesWorkDateService mesWorkDateService;

	@Autowired
	private MesDateDeviceService dateDeviceService;

	@Autowired
	private CusDeviceService deviceService;

	/**
	 * @Author Li9527
	 * @Description 工作日历-排班制
	 * @Date 2023-8-28 12:50:39
	 * @params   dept
	 * @return com.xinsoft.common.Response<?>
	 **/
	@SysLog(title = "工作日历-排班制", businessType = BusinessType.INSERT)
	@ApiOperation("工作日历-排班制")
	@PostMapping("/shift")
	public Response<?> shift(@RequestBody List<MesWorkDayDTO> mesWorkDateList) {
		// 参数校验
		for (int i = 0; i < mesWorkDateList.size(); i++) {
			MesWorkDate mesWorkDate = mesWorkDateList.get(i);
			// 查看当天是否有休息
			List<MesWorkDate> dateList = mesWorkDateService.list(new QueryWrapper<MesWorkDate>().eq("shift_date", DateUtil.date(mesWorkDate.getShiftDate())));
			for (int j = 0; j < dateList.size(); j++) {
				if(dateList.get(j).getDoRest().equals(Const.Y)){
					return Response.fail("存在已排休息的日期，请先删除该日历");
				}
			}
			mesWorkDate.setDoAll(Const.Y);
			mesWorkDate.setDoRest(Const.N);
			mesWorkDate.setDoOverTime(Const.N);
			mesWorkDateService.save(mesWorkDate);
		}
		return Response.succeed();

	}

	/**
	 * @Author Li9527
	 * @Description 工作日历-排加班
	 * @Date 2023-8-28 12:50:39
	 * @params   dept
	 * @return com.xinsoft.common.Response<?>
	 **/
	@SysLog(title = "工作日历-排加班", businessType = BusinessType.INSERT)
	@ApiOperation("排加班")
	@PostMapping("/overtime")
	public Response<?> overtime(@RequestBody MesWorkDayDTO mesWorkDate) {
		// 参数校验
		ValidatorUtils.validateEntity(mesWorkDate);
		// 查看当日是否有其他排班时间重叠的部分，如果重叠，不允许排加班
		List<MesWorkDate> dateList = mesWorkDateService.list(new QueryWrapper<MesWorkDate>().eq("shift_date", DateUtil.date(mesWorkDate.getShiftDate())));
		if(dateList.size() > 0){
			for (int i = 0; i < dateList.size(); i++) {
				if(mesWorkDate.getStartTime().getTime() >= dateList.get(i).getStartTime().getTime()
						&& mesWorkDate.getEndTime().getTime() < dateList.get(i).getEndTime().getTime()){
					return Response.fail("今日已有班制在该时间段内");
				}
			}
		}
		mesWorkDate.setDoAll(Const.N);
		mesWorkDate.setDoRest(Const.N);
		mesWorkDate.setDoOverTime(Const.Y);
		mesWorkDateService.save(mesWorkDate);
		// 解析设备id集合
		if(mesWorkDate.getMesDateDevice().size() > 0){
			for (int i = 0; i < mesWorkDate.getMesDateDevice().size(); i++) {
				MesDateDevice mesDateDevice = new MesDateDevice();
				mesDateDevice.setWorkDateId(mesWorkDate.getId());
				mesDateDevice.setDeviceId(mesWorkDate.getMesDateDevice().get(i).getDeviceId());
				dateDeviceService.save(mesDateDevice);
			}
		}
		return Response.succeed();
	}

	/**
	 * @Author Li9527
	 * @Description 工作日历-排休假
	 * @Date 2023-8-28 12:50:39
	 * @params   dept
	 * @return com.xinsoft.common.Response<?>
	 **/
	@SysLog(title = "工作日历-排休假", businessType = BusinessType.INSERT)
	@ApiOperation("排休假")
	@PostMapping("/holiday")
	public Response<?> holiday(@RequestBody MesWorkDayDTO mesWorkDate) {
		// 查看当天是否有班制
		List<MesWorkDate> dateList = mesWorkDateService.list(new QueryWrapper<MesWorkDate>().eq("shift_date", DateUtil.date(mesWorkDate.getShiftDate())));
		if(dateList.size() > 0){
			return Response.fail("今日已排班，请先删除该日历");
		}
		ValidatorUtils.validateEntity(mesWorkDate);
		mesWorkDate.setDoAll(Const.N);
		mesWorkDate.setDoRest(Const.Y);
		mesWorkDate.setDoOverTime(Const.N);
		mesWorkDateService.save(mesWorkDate);
		return Response.succeed();
	}

	/**
	 * @Author Li9527
	 * @Description 更新
	 * @Date 2023/5/17 9:57
	 * @params   dept
	 * @return com.xinsoft.common.Response<?>
	 **/
	@ApiOperation("更新")
	@SysLog(title = "工作日历-编辑", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	public Response<?> edit(@RequestBody MesWorkDayDTO mesWorkDate) {
		// 参数校验
		ValidatorUtils.validateEntity(mesWorkDate);
		return Response.succeed(mesWorkDateService.updateById(mesWorkDate));
	}

	/**
	 * @Author Li9527
	 * @Description 获取详情
	 * @Date 2023/5/17 9:57
	 * @params   id
	 * @return com.xinsoft.common.Response<?>
	 **/
	@ApiOperation("根据日期获取详情")
	@GetMapping("/detail")
	public Response<?> detail(@RequestParam(value = "date", defaultValue = "", required = true) String date) {
		// 查看当天所有排班
		List<MesWorkDate> dateList = mesWorkDateService.list(new QueryWrapper<MesWorkDate>().eq("shift_date", date + " 00:00:00"));
		List<MesWorkDayDTO> dayDTOList = new ArrayList<>();
		if(dateList.size() > 0){
			for (int i = 0; i < dateList.size(); i++) {
				MesWorkDayDTO mesWorkDayDTO = new MesWorkDayDTO();
				MesWorkDate mesWorkDate = dateList.get(i);
				BeanUtil.copyProperties(mesWorkDate,mesWorkDayDTO);
				// 如果是加班
				if(mesWorkDate.getDoOverTime().equals(Const.Y)){
					List<MesDateDevice> list = dateDeviceService.list(new QueryWrapper<MesDateDevice>().eq("work_date_id",dateList.get(i).getId()));
					for (int k = 0; k < list.size(); k++) {
						// 注入设备名称
						CusDevice device = deviceService.getById(list.get(k).getDeviceId());
						if(device != null){
							list.get(k).setDeviceName(device.getDeviceName());
						}
					}
					mesWorkDayDTO.setMesDateDevice(list);
				}
				dayDTOList.add(mesWorkDayDTO);
			}
		}
		return Response.succeed(dayDTOList);
	}

	/**
	 * @Author Li9527
	 * @Description 删除
	 * @Date 2023/5/12 9:20
	 * @params   ids
	 * @return com.xinsoft.common.Response<?>
	 **/
	@ApiOperation("删除")
	@SysLog(title = "工作日历-删除", businessType = BusinessType.DELETE)
	@PostMapping("/del")
	public Response<?> del(@RequestBody List<Integer> ids) {
		mesWorkDateService.removeByIds(ids);
		return Response.succeed();
	}

	/**
	 * @Author Li9527
	 * @Description 工作日历
	 * @Date 2023/5/18 10:51
	 * @params   param
	 * @return com.xinsoft.common.Response<?>
	 **/
	@PostMapping("/getMonthData")
	@ApiOperation("工作日历-取某月数据(默认本月）")
	public Response getMonthData(@RequestBody MesWorkDayParam param) {
		return Response.succeed(mesWorkDateService.getMonthData(param));
	}

	/**
	 * @Author Li9527
	 * @Description 工作日历
	 * @Date 2023/5/18 10:51
	 * @params   param
	 * @return com.xinsoft.common.Response<?>
	 **/
	@PostMapping("/getOverDayData")
	@ApiOperation("工作日历-取加班数据(默认本月）")
	public Response getOverDayData(@RequestBody MesWorkDayParam param) {
		return Response.succeed(mesWorkDateService.getOverDayData(param));
	}


}
